package com.tssm.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.tssm.bean.SupplyBean;
import com.tssm.common.dao.impl.BaseDAOImpl;
import com.tssm.core.util.FieldValueUtil;
import com.tssm.dao.SarcSupplierDAO;
import com.tssm.pojo.SarcSupplier;

@Repository("sarcsupplierDAO")
public class SarcSupplierDAOImpl extends BaseDAOImpl<SarcSupplier> implements
		SarcSupplierDAO {
	private static final Log LOG = LogFactory.getLog(SarcSupplierDAOImpl.class);

	/**
	 * 
	 * 
	 * 
	 * @方法说明: 根据用户名查找用户是否存在
	 * @param account
	 *            用户名
	 * @return
	 * @作者:徐龙
	 * @日期:2012-1-11
	 */
	public SarcSupplier findUserIsExist(String account) {
		SarcSupplier supplier = null;
		String hql = "from SarcSupplier ss where ss.dr=0 and ss.accountState=0 and ss.account=?";
		Object[] obj = { account };
		List<SarcSupplier> nflist = super.find(hql, obj);
		if (nflist.size() > 0) {
			supplier = nflist.get(0);
		}
		return supplier;
	}

	/**
	 * 
	 * 
	 * 
	 * @方法说明: 查找E-mail是否已被使用
	 * @param email
	 * @return
	 * @作者:徐龙
	 * @日期:2012-1-11
	 */
	public boolean findEmailIsExist(String email) {
		boolean flag = false;
		String hql = "from SarcSupplier ss where ss.dr=0 and ss.accountState=0 and ss.email=? ";
		Object[] obj = { email };
		List<SarcSupplier> nflist = super.find(hql, obj);
		if (nflist.size() > 0) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 
	 * 
	 * 
	 * @方法说明: 根据用户名和密码查找
	 * @param account
	 *            用户名 或 企业名称
	 * @param passowrd
	 *            用户密码
	 * @return
	 * @作者:徐龙
	 * @日期:2012-1-15
	 */
	public SarcSupplier findByUserAccount(String account, String password) {
		SarcSupplier supplier = null;
		String hql = "from SarcSupplier ss where ss.dr=0 and ss.accountState=0 and (ss.account=? or ss.supplierName=?)  and ss.password=?";
		Object[] obj = { account, account, password };
		List<SarcSupplier> nflist = super.find(hql, obj);
		if (nflist.size() > 0) {
			supplier = nflist.get(0);
		}
		return supplier;
	}

	@SuppressWarnings("unchecked")
	public List<SupplyBean> findAllSupply() {
		return (List<SupplyBean>) getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws SQLException, HibernateException {
						String sql = "select ss.SUPPLIERID as id,ss.ACCOUNT as account,ss.PASSWORD as password,ss.EMAIL as email from  SARC_SUPPLIER as ss ";
						SQLQuery query = session.createSQLQuery(sql);
						query.addScalar("id", Hibernate.LONG);
						query.addScalar("account");
						query.addScalar("password");
						query.addScalar("email");
						query
								.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
						List<SupplyBean> objlist = query.list();
						SupplyBean supplybean = new SupplyBean();
						FieldValueUtil<SupplyBean> valueutil = new FieldValueUtil<SupplyBean>();
						List<SupplyBean> sslist = new ArrayList<SupplyBean>();
						try {
							sslist = valueutil.setObjectList(objlist,
									supplybean);
						} catch (Exception e) {
							LOG.info("查询供应商信息" + sql + "出现异常:" + e);
						}
						return sslist;
					}
				});
	}

}
